﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Web.Security;
namespace Quality.BasicData
{
    public partial class usersedit : Form
    {
        public String id { get; set; }
        public usersedit(string id)
        {
            this.id = id;
            InitializeComponent();
        }
        private void usersedit_Load(object sender, EventArgs e)
        {
            if (id == "") {
                return;
            }
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = new SqlCommand();

            da.SelectCommand.CommandText = "select * from  users a left join organization b on a.org=b.id where a.id=@id";
            da.SelectCommand.Parameters.AddWithValue("@id", id);
            DataSet dt = DB.getlist(da);
            DataRow row = dt.Tables[0].Rows[0];
            userlogin.Text = row["userlogin"].ToString();
            username.Text = row["username"].ToString();
            org.Text = row["nodeName"].ToString();
            org.Tag = row["org"].ToString();

            telephone.Text = row["telephone"].ToString();


            stopcheckBox.Checked = Convert.ToBoolean(row["stop"]);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            selectorg frm = new selectorg();

            frm.WindowState = FormWindowState.Normal;
            frm.Dock = DockStyle.Fill;
            frm.TransfEvent += frm_TransfEvent;
            frm.Show();
        }
        //事件处理方法
        void frm_TransfEvent(string[] value)
        {
            org.Tag = value[0];
            org.Text = value[1];

        }
        public event TransfDelegate1 TransfEvent;
        private void toolStripButton1_Click(object sender, EventArgs e)

         {
            if(string.IsNullOrEmpty(username.Text)){ MessageBox.Show("姓名不能为空！"); return; }
            if (string.IsNullOrEmpty(userlogin.Text)) { MessageBox.Show("用户名不能为空！"); return; }
            if (string.IsNullOrEmpty(org.Text)) { MessageBox.Show("部门不能为空！"); return; }


            SqlCommand comm = new SqlCommand();
            if (id == "")
            {
               int count= sqlHelper.Count("select * from users where userlogin='"+userlogin.Text+"'");
               if (count > 0) { MessageBox.Show("登录名重复，不能保存！"); return; }


               comm.CommandText = "insert into users(username,userlogin,password,org,telephone,stop)values(@username,@userlogin,@password,@org,@telephone,@stop)";
            }
            else
            {

                comm.CommandText = "update users set username=@username,userlogin=@userlogin,org=@org,telephone=@telephone,stop=@stop where id=@id";


            }

            comm.Parameters.AddWithValue("@id", id);
            comm.Parameters.AddWithValue("@username", username.Text);
            comm.Parameters.AddWithValue("@userlogin", userlogin.Text);
            comm.Parameters.AddWithValue("@password",FormsAuthentication.HashPasswordForStoringInConfigFile("", "md5"));
            comm.Parameters.AddWithValue("@org", org.Tag == null ? "" : org.Tag);
            comm.Parameters.AddWithValue("@telephone", telephone.Text);
            comm.Parameters.AddWithValue("@stop", stopcheckBox.Checked);
            DB.dosql(comm);
            TransfEvent();
            MessageBox.Show("保存成功！");
            this.Close();
            
         
        }

    

    }
}
